package com.example.gkcircle.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
 * Created by Jehu on 16/4/8.
 */

public class DBHelper extends SQLiteOpenHelper {

    public static final String DATA_BASE_NAME = "GKCIRCLE_DB";
    public static final int DATA_BASE_VERSION = 1;
    public static final String WISH_TABLE_NAME = "wish";
    public static final String BOOKSHELF_TABLE_NAME="bookshelf";

    private SQLiteDatabase mDb;

    public DBHelper(Context context) {
        super(context, DATA_BASE_NAME, null, DATA_BASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        onCreateWishTable(db);
        onCreateBookshelfTable(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public interface WishTable{
        String _ID = "_id";
        String IS_LIKE="is_like";
        String USER_ID="user_id";
        String OBJECT_ID = "objectid";

    }

    private void onCreateWishTable(SQLiteDatabase db) {
        StringBuilder str = new StringBuilder();
        str.append("CREATE TABLE IF NOT EXISTS ")
                .append(DBHelper.WISH_TABLE_NAME).append(" ( ").append(WishTable._ID)
                .append(" INTEGER PRIMARY KEY AUTOINCREMENT,")
                .append(WishTable.USER_ID).append(" varchar(30),")
                .append(WishTable.OBJECT_ID).append(" varchar(30),")
                .append(WishTable.IS_LIKE).append(" Integer);");
        db.execSQL(str.toString());
    }


    public interface BookshelfTable{
        String _ID = "_id";
        String USER_ID="user_id";
        String BOOK_URL="book_url";
        String BOOK_NAME="bookname";
        String AUTHOR="author";
        String PUBLISH="publish";
        String CALL_NUMBER="call_number";
        String IMAGE_URL="image_url";
    }

    private void onCreateBookshelfTable(SQLiteDatabase db) {
        StringBuilder str = new StringBuilder();
        str.append("CREATE TABLE IF NOT EXISTS ")
                .append(DBHelper.BOOKSHELF_TABLE_NAME).append(" ( ").append(BookshelfTable._ID)
                .append(" INTEGER PRIMARY KEY AUTOINCREMENT,")
                .append(BookshelfTable.USER_ID).append(" varchar(30),")
                .append(BookshelfTable.BOOK_URL).append(" varchar(50),")
                .append(BookshelfTable.BOOK_NAME).append(" varchar(30),")
                .append(BookshelfTable.AUTHOR).append(" varchar(30),")
                .append(BookshelfTable.PUBLISH).append(" varchar(30),")
                .append(BookshelfTable.CALL_NUMBER).append(" varchar(30),")
                .append(BookshelfTable.IMAGE_URL).append(" varchar(50));");
        db.execSQL(str.toString());
    }




    /**
     * 获取数据库操作
     *
     * @param isWrite
     *            是否可写
     * @return
     */
    public synchronized SQLiteDatabase getDatabase(boolean isWrite) {

        if (mDb == null || !mDb.isOpen()) {
            if (isWrite) {
                try {
                    mDb = getWritableDatabase();
                } catch (Exception e) {
                    //  当数据库不可写时
                    mDb = getReadableDatabase();
                    return mDb;
                }
            } else {
                mDb = getReadableDatabase();
            }
        }
        // } catch (SQLiteException e) {
        // // 当数据库不可写时
        // mDb = getReadableDatabase();
        // }
        return mDb;
    }

    public int delete(String table, String whereClause, String[] whereArgs) {
        getDatabase(true);
        return mDb.delete(table, whereClause, whereArgs);
    }

    public long insert(String table, String nullColumnHack, ContentValues values) {
        getDatabase(true);
        return mDb.insertOrThrow(table, nullColumnHack, values);
    }

    public int update(String table, ContentValues values, String whereClause,
                      String[] whereArgs) {
        getDatabase(true);
        return mDb.update(table, values, whereClause, whereArgs);
    }

    public Cursor rawQuery(String sql, String[] selectionArgs) {
        getDatabase(false);
        return mDb.rawQuery(sql, selectionArgs);
    }

    public void execSQL(String sql) {
        getDatabase(true);
        mDb.execSQL(sql);
    }

    public Cursor query(String table, String[] columns, String selection,
                        String[] selectionArgs, String groupBy, String having,
                        // final
                        String orderBy) {
        getDatabase(true);
        return mDb.query(table, columns, selection, selectionArgs, groupBy,
                having, orderBy);
    }

}
